﻿using System;
using System.Collections.Generic;
using System.Text;

namespace SICOMModbus
{
    /// <summary>
    /// ModbusTCP/IP数据帧
    /// </summary>
    public class ModbusTCP_ADU
    {
        ModbusTCPMBAP mBAP = new ModbusTCPMBAP();
        /// <summary>
        /// Modbus协议报文头
        /// </summary>
        public ModbusTCPMBAP MBAP
        {
            get { return mBAP; }
            set { mBAP = value; }
        }

        private byte functionCode;
        /// <summary>
        /// 功能码
        /// </summary>
        public byte FunctionCode
        {
            get { return functionCode; }
            set { functionCode = value; }
        }

        private byte[] data;
        /// <summary>
        /// 数据
        /// </summary>
        public byte[] Data
        {
            get { return data; }
            set { data = value; }
        }

        public byte[] getADUBytes()
        {
            byte[] Bytes = new byte[8 + data.Length];
            for (int i = 0; i < 7; i++)
            {
                Bytes[i] = mBAP.getBytes()[i];
            }
            Bytes[7] = FunctionCode;
            for (int i = 0, j = 8; i < Data.Length; i++, j++)
            {
                Bytes[j] = Data[i];
            }
            return Bytes;
        }
    }
}
